import { PageParam } from '@/services/common'
import { covertTableData } from '@/utils/tableUtil'
import { request } from '@umijs/max'

export interface TargetVO {
  /**
   * id
   */
  id: string
  /**
   * 日期
   */
  month: string
  /**
   * 类型
   */
  type: string
  /**
   * 用户id
   */
  userId: string
  /**
   * 目标
   */
  target: number
  /**
   * 单位
   */
  unit: string
}

export interface TargetQueryDTO extends PageParam {
  /**
   * id
   */
  id: string
  /**
   * 日期
   */
  month: string
  /**
   * 类型
   */
  type: string
  /**
   * 用户id
   */
  userId: string
  /**
   * 目标
   */
  target: number
  /**
   * 单位
   */
  unit: string
}

export interface TargetUpdateDTO {
  /**
   * id
   */
  id?: string
  /**
   * 日期
   */
  month?: string
  /**
   * 类型
   */
  type?: string
  /**
   * 用户id
   */
  userId?: string
  /**
   * 目标
   */
  target?: number
  /**
   * 单位
   */
  unit?: string
}

export const getTargetById = async (id: string) => {
  return request<TargetVO>(`/api/target?id=${id}`, { method: 'GET' })
}

export const getTargetList = async (params: TargetQueryDTO) => {
  return request<Array<TargetVO>>(`/api/target/list`, { method: 'GET', params })
}

export const getTargetPage = async (params: TargetQueryDTO) => {
  let response = await request<Array<TargetVO>>(`/api/target/page`, { method: 'GET', params })
  return covertTableData(response)
}

export const updateTarget = async (targetUpdateDTO: TargetUpdateDTO) => {
  return request<TargetVO>(`/api/target/update`, { method: 'POST', data: targetUpdateDTO })
}

export const saveTarget = async (targetUpdateDTO: TargetUpdateDTO) => {
  return request<TargetVO>(`/api/target/save`, { method: 'POST', data: targetUpdateDTO })
}

export const removeTarget = async (id: string) => {
  return request<boolean>(`/api/target/remove?id=${id}`, { method: 'POST' })
}

export const getCustomerChannelTarget = () => {
  return request<TargetVO>(`/api/target/getCustomerChannelTarget`)
}
